package com.sina.finance.api.framework.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.sina.finance.api.framework.dao.IDemoAPIDao;
import com.sina.finance.api.framework.domain.DemoOrder;
import com.sina.finance.api.framework.domain.DemoShip;
import com.sina.finance.api.framework.domain.DemoShipItem;
import com.sina.finance.api.framework.response.DemoGetResponse;
import com.sina.finance.api.framework.response.Response;
import com.sina.finance.api.framework.service.IAPIService;

public class DemoAPIServiceImpl implements IAPIService {

	private IDemoAPIDao adapterDao;

	public IDemoAPIDao getAdapterDao() {
		return adapterDao;
	}

	public void setAdapterDao(IDemoAPIDao adapterDao) {
		this.adapterDao = adapterDao;
	}

	@Override
	public Response doJob(Map<String, String[]> paramMap) {
		
		DemoGetResponse response = new DemoGetResponse();
		
		response.setTotalResults(this.getShipCount());

		List<DemoShip> ships = this.getShips();
		
		Iterator<DemoShip> iterator = ships.iterator();
		
		while(iterator.hasNext()){
			DemoShip ship = iterator.next();
			ship.setOrder(this.getOrderByOrderGUID(ship.getOrderGUID()));
			ship.setShipItems(this.getShipItemsByShipGUID(ship.getShipGUID()));
		}
		
		response.setShips(ships);
		
		return response;
	}
	
	private int getShipCount(){
		return adapterDao.getShipCount();
	}
	
	private List<DemoShip> getShips(){
		return adapterDao.getShips();
	}
	
	private List<DemoShipItem> getShipItemsByShipGUID(String shipGUID){
		return adapterDao.getShipItemsByShipGUID(shipGUID);
	}
	
	private DemoOrder getOrderByOrderGUID(String orderGUID){
		return adapterDao.getOrderByOrderGUID(orderGUID);
	}
}
